<?php

header("Content-Type: text/html;charset=utf8");

class CodeDb
{

    private $mysql;

    public function __construct(InMysql $mysql = null)
    {
        if ($mysql == null) {
            $this->mysql = new InMysql();
        } else {
            $this->mysql = $mysql;
        }
    }

    /**
     * 激活码列表
     * @param type $cs
     * @param type $isActive
     * @param type $page
     * @param type $pageSize
     * @param type $keyword
     * @return type
     */
    public function listCode($cs, $isActive, $page, $pageSize, $type, $keyword = "")
    {
        $start = $page * $pageSize;
        $where = " where yid > 0 ";
        if ($cs > -1) {
            $where .= " and Cs = " . $cs;
        }
        if ($isActive > -1) {
            $where .= " and isact = " . $isActive;
        }
        if ($keyword == "") {
            $sql = "select * from yct " . $where . " limit " . $start . "," . $pageSize;
            $sql2 = "select * from yct " . $where;
        } else {
            if ($type == "key") {
                $where .= " and keyvalue like '%" . $keyword . "%'";
            } else if ($type == "mac") {
                $where .= " and mac like '%" . $keyword . "%'";
            } else if ($type == "info") {
                $where .= " and info like '%" . $keyword . "%'";
            }
            $sql = "select * from yct " . $where . " limit " . $start . "," . $pageSize;
            $sql2 = "select * from yct " . $where;
        }

        $data['data'] = $this->mysql->fetchData($sql);
        $data['count'] = count($this->mysql->fetchData($sql2));
        return $data;
    }

    /**
     * 创建激活码
     * @param type $cs 第几册
     * @param type $exp 有效时间
     * @param type $count 数量
     * @return type
     */
    public function createCode($cs, $exp, $count)
    {
        $sqls = array();
        $data = array();
        for ($i = 0; $i < $count; $i++) {
            do {
                $code = $this->makCode();
            } while ($this->codeExists($code));
            $dataValue = array("keyvalue" => $code, "vol" => $cs, "exptime" => $exp);
            $sql = "insert into yct(keyvalue,cs,exptime,updatetime) values('" . $code . "'," . $cs . ",'" . $exp . "',UNIX_TIMESTAMP())";
            array_push($sqls, $sql);
            array_push($data, $dataValue);
        }
        $dat = $this->mysql->executeBatch($sqls);
        if ($dat) {
            return $data;
        }

    }

    /**
     * 获取学生激活码
     * @return type
     */
    public function getStudentCode($page, $pageSize)
    {
        $start = $page * $pageSize;
        $sql = "select * from active_code order by id desc limit " . $start . "," . $pageSize ;
        $sql2 = "select * from active_code order by id desc ";
        $data['data'] = $this->mysql->fetchData($sql);
        $data['count'] = count($this->mysql->fetchData($sql2));
        return $data;


    }

    /**
     * 创建获取学生激活码
     * @return type
     */
    public function createCodeS($codeNumber, $num,$exp, $desc,$acttime){
        $sqls = array();
        $data = array();
        for ($i = 0; $i < $codeNumber; $i++) {
            do {
                $code = $this->makCodeS();
            } while ($this->codeExistsS($code));
            $dataValue = array("active_code" => $code,"exptime" => $exp,"max_use_times"=>$num,"info"=>$desc);
            $sql = "insert into active_code(active_code,exptime,info,create_time,max_use_times) values('$code','$exp','$desc','$acttime','$num')";
            array_push($sqls, $sql);
            array_push($data, $dataValue);
        }
        $dat = $this->mysql->executeBatch($sqls);
        if ($dat) {
            return $data;
        }


    }

    /**
     * 激活码延期操作
     * @param type $id 该激活码的id
     * @param type $endTime 到期时间
     * @return type
     */
    public function updateDelay($id, $endTime)
    {
        $sql = "update yct set endtime = '" . $endTime . "' where yid = " . $id;
        return $this->mysql->executeData($sql);


    }

    /**
     *
     * @global type $insert_wrong
     * @param type $cs_string
     * @param type $exp
     * @param type $info
     * @return \type
     */
    public function createGroupKeys($cs_string, $exp, $info)
    {
        $array = $cs_string;
        $res_array = array();
        $keys = "";
        for ($i = 0, $len = count($array); $i < $len; $i++) {
            $cs_value = $array[$i];
            if ($cs_value == "1") {
                do {
                    $code = $this->MakCode();
                } while ($this->IsCreated($code));
                $cs = $i;
                $key = "('$code',$cs,$exp,'$info')";
                $keys = $keys == "" ? $key : $keys . "," . $key;
                $res_array[$i]['keyValue'] = $code;
                $res_array[$i]['vol'] = $cs;
            } else {
                $res_array[$i] = "";
            }
        }
        $sql = "insert into yct(keyvalue,cs,exptime,info) values $keys";

        $db = getmysql();
        $r = $db->exec($sql);
        if ($r) {
            $res['code'] = 0;
            $res['data'] = $res_array;
            $res['info'] = $info;
        } else {
            $res['code'] = 1;
            $res['cs'] = $array;
            $res['msg'] = "sql错误" . $sql;
        }
        $db = null;
        return $res;
    }


    /**
     * 激活码是否存在
     * @param type $code
     * @return boolean
     */
    private function codeExists($code)
    {
        $sql = "select yid from yct where keyvalue = '" . $code . "'";
        $array = $this->mysql->fetchData($sql);
        if (count($array) > 0) {
            return true;
        }
        return false;
    }

    /**
     * 学生激活码是否存在
     * @param type $code
     * @return boolean
     */
    private function codeExistsS($code)
    {
        $sql = "select active_code from active_code where active_code = '" . $code . "'";
        $array = $this->mysql->fetchData($sql);
        if (count($array) > 0) {
            return true;
        }
        return false;
    }
    /**
     *
     * @global type $insert_wrong
     * @param type $cs_string
     * @param type $exp
     * @param type $info
     * @param type $mac
     * @return \type
     */
    public function createGroupKeysActive($cs_string, $exp, $info, $mac, $acttime, $endtime)
    {
        $array = $cs_string;

        $res_array = array();
        $keys = "";
        for ($i = 0, $len = count($array); $i < $len; $i++) {
            $cs_value = $array[$i];
            if ($cs_value == "1") {
                do {
                    $code = $this->MakCode();
                } while ($this->IsCreated($code));
                $cs = $i;
                $key = "('$code',$cs,$exp,'$info','$mac','$acttime','$endtime',1)";
                $keys = $keys == "" ? $key : $keys . "," . $key;
                $res_array[$i]['keyValue'] = $code;
                $res_array[$i]['vol'] = $cs;
            } else {
                $res_array[$i] = "";
            }
        }

        $col = "yct(keyvalue,cs,exptime,info,mac,acttime,endtime,isact)";
        $sql = "insert into $col values $keys";

        $db = getmysql();
        $r = $db->exec($sql);
        if ($r) {
            $res['code'] = 0;
            $res['data'] = $res_array;
            $res['info'] = $info;
        } else {
            $res['code'] = 1;
            $res['msg'] = "sql错误" . $sql;
        }
        $db = null;
        return $res;
    }


    /*     * 序列号是否存在
 * @param $key
 */

    private static function IsCreated($key)
    {
        $db = getmysql();
        $rs = $db->query("select * from yct where keyvalue='$key' ");
        if ($rs) {
            $rs->setFetchMode(PDO::FETCH_ASSOC);
            $yct = $rs->fetch();
            if ($yct) {
                $res = true;
            } else {
                $res = false;
            }

            $db = null;
            return $res;
        }
    }

    /**
     * 生成激活码
     * @return type
     */
    private function makCode()
    {
        $now = time();
        $seed = rand(1, 1000);
        $s = md5($now . $seed);
        return strtoupper(substr($s, rand(0, 15), 15));
    }
    /**
     * 生成学生激活码
     * @return type
     */
    private function makCodeS()
    {
        $now = time();
        $seed = rand(1, 1000);
        $s = md5($now . $seed);
        return strtoupper(substr($s, rand(0, 15), 10));
    }

}
